<template>
    <view class="page">
        <Header> 提交订单 </Header>
        <div class="info">
            <div class="addr fx aifs" v-if="addr.Id" @click="$go('address')">
                <div class="icon">
                    <Nimg src="032.png" width="31" height="43" />
                </div>
                <div class="ms fx1">
                    <div class="tt fx">
                        <div class="name">{{ addr.name }}</div>
                        <div class="phone">{{ addr.tel }}</div>
                    </div>
                    <div class="bb">{{ addr.addr }}</div>
                </div>
                <div class="jt">
                    <Nimg src="033.png" width="17" height="33" />
                </div>
            </div>
            <div class="addAddr fx" v-else @click="$go('addAddress')">
                <div class="icon">
                    <Nimg src="032.png" width="31" height="43" />
                </div>
                <div class="ms fx1">添加收货地址</div>
                <div class="jt">
                    <Nimg src="033.png" width="17" height="33" />
                </div>
            </div>

            <div class="pro">
                <div class="list">
                    <div
                        class="item fx"
                        v-for="(i, index) in list"
                        :key="index"
                    >
                        <div class="img">
                            <Nimg
                                :src="i.thumb_src + i.photo.split(',')[0]"
                                width="188"
                                height="188"
                            />
                        </div>
                        <div class="ms fx1">
                            <div class="name twoLine">
                                {{ i.name }}
                            </div>
                            <div class="bot fx">
                                <div class="price">￥{{ i.price }}</div>
                                <div class="fx1"></div>
                                <div class="num">× {{ num }}</div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="datas">
                    <div class="line fx jcc">
                        <div class="name">商品金额</div>
                        <div class="fx1"></div>
                        <div class="val">￥{{ heji }}</div>
                    </div>
                    <!-- <div class="line fx jcc jfdk">
                        <div class="name">积分抵扣</div>
                        <div class="fx1"></div>
                        <div class="val">-￥10.00</div>
                    </div> -->
                    <div class="sfk fx">
                        <div class="fx1"></div>
                        实付款
                        <div class="val">￥{{ heji }}</div>
                    </div>
                </div>
            </div>
            <div class="huizong fx">
                <div class="fx1"></div>
                <div class="total">共{{ num }}件</div>
                <div class="heji fx">
                    合计
                    <div class="price">￥{{ heji }}</div>
                </div>
                <div class="btn fx jcc" @click="submitNow">马上支付</div>
            </div>
        </div>
    </view>
</template>

<script>
import $ from "@/common";
export default {
    data() {
        return {
            id: "",
            num: 1,
            addr: {},
            list: [],
            heji: 0,
            product_allshowflag: 0,
            product_showallnum: 0,
            out_trade_no: "",
            us: {},
            captain_id: "",
        };
    },
    methods: {
        getList() {
            let url = $.product("productDetail");
            let data = {
                product_id: this.id,
            };
            $.post(url, data).then((res) => {
                if (res.isError) {
                    return;
                }
                this.list = [res];
                this.setHeji();
            });
        },
        setHeji() {
            let heji = 0;
            this.list.forEach((ele) => {
                heji += (ele.price * 10000 * this.num) / 10000;
            });
            this.heji = heji;
        },
        submitNow() {
            const gogo = () => {
                if (this.out_trade_no) {
                    this.payNow();
                    return;
                }
                let url = $.shopping("addOrder");
                let pro = this.list[0];

                let data = {
                    product_id: this.id,
                    product_name: pro.name,
                    product_num: this.num,
                    addr_id: this.addr.Id,
                    price: pro.price,
                    total_price: this.heji,
                    type: 1,
                };

                if (this.captain_id) {
                    data.captain_id = this.captain_id;
                }

                $.post(url, data).then((res) => {
                    if (res.isError) {
                        return;
                    }
                    this.out_trade_no = res.out_trade_no;
                    this.payNow();
                });
            };

            if (!this.addr.Id) {
                $.tip("请选择收货地址");
                return;
            }

            let tmplIds = [
                "haU--yTSWt4SuaWxzrpRu8sig9cQJcST_bD9mitm0Mg",
                "8Kkh2QGPUes-IMoemtZKHeZBEohHgeohND3ji1MeKS0",
            ];

            wx.requestSubscribeMessage({
                tmplIds,
                complete: () => {
                    gogo();
                },
            });
        },
        payNow() {
            let out_trade_no = this.out_trade_no;
            let openid = $.getLocal("openid");
            let total_fee = Number(this.heji) * 100;
			let appid = $.appid

            let data = {
                openid,
                total_fee,
                out_trade_no,
				appid
            };

            $.get($.domain + "/wxpay.php", data).then((res) => {
                let data = res;

                $.log("okle", data);
                $.hideLoading();
                wx.requestPayment({
                    timeStamp: data.timeStamp,
                    nonceStr: data.nonceStr,
                    package: data.package,
                    signType: data.signType,
                    paySign: data.paySign,
                    success: (res) => {
                        $.suc("支付成功");
                        setTimeout(() => {
                            this.$switchTab("dingdan");
                        }, 1000);
                    },
                    fail: (res) => {
                        $.tip("支付未成功");
                        $.log(res);
                    },
                });
            });
        },
        getAddr() {
            let url = $.user("receiveDefAddr");
            $.post(url).then((res) => {
                if (res.isError) {
                    return;
                }
                this.addr = res;
            });
        },
    },
    onLoad(op) {
        this.id = op.id;
        this.num = op.num;
        $.getUsInfo().then((us) => {
            this.us = us;
            this.captain_id = us.father_id || "";
        });
    },
    onShow() {
        this.getList();
        this.getAddr();
    },
};
</script>

<style lang="less" scoped>
.page {
    background: #fafafa;
    height: 100vh;
    font-size: 26rpx;
    overflow: hidden;
}

.info {
    height: calc(100vh - 180rpx);
    overflow: auto;

    .addr {
        width: 686rpx;
        height: 160rpx;
        background: #ffffff;
        box-shadow: 0rpx 1rpx 9rpx 0rpx rgba(0, 0, 0, 0.08);
        border-radius: 16rpx;
        margin: 0 auto;
        margin-top: 33rpx;
        padding: 0 23rpx;
        padding-top: 38rpx;
        .icon {
            margin-right: 20rpx;
        }
        .ms {
            color: #666666;
            .tt {
                .name {
                    margin-right: 10rpx;
                }
            }
            .bb {
                margin-top: 10rpx;
                font-size: 26rpx;
            }
        }
        .jt {
            margin-top: 30rpx;
            margin-right: 30rpx;
        }
    }

    .addAddr {
        width: 686rpx;
        height: 160rpx;
        background: #ffffff;
        box-shadow: 0rpx 1rpx 9rpx 0rpx rgba(0, 0, 0, 0.08);
        border-radius: 16rpx;
        margin: 0 auto;
        margin-top: 33rpx;
        padding: 0 23rpx;
        .icon {
            margin-right: 20rpx;
        }
        .ms {
            color: #666666;
            .tt {
                .name {
                    margin-right: 10rpx;
                }
            }
            .bb {
                margin-top: 10rpx;
                font-size: 26rpx;
            }
        }
        .jt {
            margin-right: 30rpx;
        }
    }

    .pro {
        width: 686rpx;
        background: #ffffff;
        border-radius: 16rpx;
        box-shadow: 0rpx 1rpx 9rpx 0rpx rgba(0, 0, 0, 0.08);
        margin: 0 auto;
        margin-top: 36rpx;
        .datas {
            padding: 30rpx;
            .line {
                padding: 10rpx;
                font-weight: bold;
                .name {
                    color: #666666;
                    font-size: 26rpx;
                }
                &.jfdk {
                    .val {
                        color: #ff0000;
                        font-size: 30rpx;
                    }
                }
            }
        }
        .sfk {
            margin-top: 40rpx;
            font-weight: bold;
            font-size: 28rpx;
            .val {
                color: #ff0000;
                font-size: 34rpx;
            }
        }
        .item {
            padding: 30rpx 20rpx;
            border-bottom: 2rpx solid #dddddd;
            .img {
                border-radius: 16rpx;
                overflow: hidden;
            }
            .ms {
                padding-left: 10rpx;
                .name {
                    font-size: 26rpx;
                    font-family: PingFang SC;
                    font-weight: bold;
                    color: #333333;
                }
                .types {
                    margin-top: 20rpx;
                    .type {
                        min-width: 110rpx;
                        padding: 0 20rpx;
                        height: 40rpx;
                        border: 1rpx solid #dddddd;
                        border-radius: 20rpx;
                        color: #999999;
                        font-size: 22rpx;
                        margin-left: 20rpx;
                        &:first-child {
                            margin-left: 0;
                        }
                    }
                }
                .bot {
                    margin-top: 20rpx;
                    .price {
                        font-size: 30rpx;
                        font-weight: bold;
                        color: #ff0000;
                    }
                    .num {
                        font-size: 26rpx;
                        font-weight: bold;
                        color: #666666;
                    }
                }
            }
        }
    }
}

.title {
    padding-left: 60rpx;
    font-size: 32rpx;
    flex: 1;
}

.huizong {
    background: #fff;
    padding: 20rpx;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    padding-bottom: 50rpx;
    .select {
        padding: 28rpx;
    }

    .total {
        font-size: 26rpx;
        color: #666666;
        padding-right: 10rpx;
    }

    .heji {
        font-weight: bold;
        font-size: 24rpx;
        padding-right: 30rpx;
        .price {
            font-size: 34rpx;
            color: #ff0000;
            padding-left: 10rpx;
        }
    }
    .btn {
        width: 220rpx;
        height: 86rpx;
        background: #ff7e02;
        color: #fff;
        border-radius: 100rpx;
        font-size: 28rpx;
        font-weight: bold;
    }
}
</style>

